package lk.icta.common.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.BranchDAO;
import lk.icta.common.dao.PreloadableDAO;
import lk.icta.common.entity.Branch;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kusal Samarakoon
 */
public class BranchDAOImpl extends BaseDAO implements BranchDAO, PreloadableDAO {

     private final Map<Integer, String> branchList = new HashMap<Integer, String>();

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void addBranch(Branch branch) {
        logger.debug("Adding a Branch [Id: {}\tName: {}]", branch.getId(), branch.getEnBranchName());
        em.persist(branch);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public Branch getBranchById(int branchId) {
        logger.debug("Loading Branch {}", branchId);
        return em.find(Branch.class, branchId);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void updateBranch(Branch branch) {
        logger.debug("Updating a Branch [Id: {}\tName: {}]", branch.getId(), branch.getEnBranchName());
        em.merge(branch);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Branch> getAllBranchNames() {
        Query q = em.createNamedQuery("findAllBranches");
        return q.getResultList();
    }

    public Map<Integer,String> getBranchList(String language){
        logger.debug("getting branch list :{}",branchList.size());
        if (AppConstants.SINHALA.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_BRANCHES + 1); i++) {
               branchList.put(i,this.getBranchById(i).getSiBranchName());
            }
        } else if (AppConstants.ENGLISH.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_BRANCHES + 1); i++) {
                branchList.put(i,this.getBranchById(i).getEnBranchName());
            }
        } else if (AppConstants.TAMIL.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_BRANCHES + 1); i++) {
                branchList.put(i,this.getBranchById(i).getTaBranchName());
            }
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }

        if (branchList != null)
            return branchList;
        else
            return Collections.emptyMap();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void preload() {
        logger.debug("Call getting branch preload");
        Query query = em.createQuery("SELECT p FROM Branch p order by p.enBranchName");
        List<Branch> braList = query.getResultList();
        for (Branch r : braList) {
            int branchId = r.getId();
            String name = r.getEnBranchName();
            branchList.put(branchId, name);
             logger.debug("getting branch id :{} Name :{}",branchId,name);
        }
    }
}
